package com.t282.crm.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t282.crm.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Chen
 * @since 2021-05-19
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private RestTemplate restTemplate;

    @ResponseBody
    @RequestMapping("/postProduct")
    public String postProduct( @RequestParam(defaultValue = "1",required = false) int pageIndex,
                               String prodName,
                               String prodType,
                               String prodBatch){

        System.out.println(prodName);
        String url ="http://localhost:8081/sale/saleproduct/page2?pageIndex="+pageIndex;
        try {
            if (prodName!=null){
                prodName = URLDecoder.decode(URLDecoder.decode(prodName,"UTF-8"),"utf-8");
                url+="&prodName="+prodName;
            }
            System.out.println(prodName);
            if (prodType!=null){
                prodType = URLDecoder.decode(URLDecoder.decode(prodType,"UTF-8"),"utf-8");
                url+="&prodType="+prodType;
            }
            if (prodBatch!=null){
                prodBatch = URLDecoder.decode(URLDecoder.decode(prodBatch,"UTF-8"),"utf-8");
                url+="&prodBatch="+prodBatch;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        String page = restTemplate.getForObject(
                url
                , String.class);
//        String page = restTemplate.getForObject(
//                "http://localhost:8081/sale/saleproduct/page/"+pageIndex+
//                        ""+prodName+""+prodType+""+prodBatch
//                , String.class);

//        String page = restTemplate.postForObject(
//                "http://localhost:8081/sale/saleproduct/page2",request
//                , String.class);


        return page;
    }
    @GetMapping("/get")
    public String get(){
        return"/product/list";
    }
}

